#!/bin/bash
# Electron Cookie提取工具运行脚本

# 颜色设置
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

# 显示标题
echo -e "${BLUE}============================================${NC}"
echo -e "${GREEN}     Electron Cookie提取工具 v1.0          ${NC}"
echo -e "${BLUE}============================================${NC}"

# 检查依赖
echo -e "${YELLOW}正在检查依赖...${NC}"

# 检查Node.js
if ! command -v node &> /dev/null; then
    echo -e "${RED}错误: 未找到 node 命令${NC}"
    echo -e "请安装 Node.js 14.0 或更高版本"
    exit 1
fi

# 检查依赖包是否安装
if [ ! -d "node_modules" ]; then
    echo -e "${YELLOW}正在安装依赖包...${NC}"
    pnpm install
    if [ $? -ne 0 ]; then
        echo -e "${RED}安装依赖包失败${NC}"
        exit 1
    fi
fi

echo -e "${GREEN}依赖检查完成!${NC}"

# 检查index.js文件是否存在
if [ ! -f "index.js" ]; then
    echo -e "${RED}错误: index.js 文件不存在${NC}"
    exit 1
fi

# 为index.js添加执行权限
chmod +x index.js

# 显示菜单
show_menu() {
    echo -e "${BLUE}============================================${NC}"
    echo -e "${GREEN}请选择操作:${NC}"
    echo -e "1) 查找Electron应用进程"
    echo -e "2) 按名称查找进程"
    echo -e "3) 从进程提取Cookie"
    echo -e "4) 仅从文件提取Cookie"
    echo -e "5) 仅从内存提取Cookie"
    echo -e "6) 帮助信息"
    echo -e "0) 退出"
    echo -e "${BLUE}============================================${NC}"
    echo -n "请输入选项 [0-6]: "
}

# 主循环
while true; do
    show_menu
    read choice
    
    case $choice in
        1) 
            echo -e "${YELLOW}查找Electron应用进程...${NC}"
            node index.js find -d
            echo -e "${GREEN}完成!${NC}"
            ;;
        2)
            echo -n "请输入进程名称: "
            read process_name
            echo -e "${YELLOW}查找名称包含 '$process_name' 的进程...${NC}"
            node index.js find -n "$process_name" -d
            echo -e "${GREEN}完成!${NC}"
            ;;
        3)
            echo -n "请输入目标进程PID: "
            read pid
            echo -e "${YELLOW}从进程 $pid 提取Cookie...${NC}"
            node index.js extract $pid
            echo -e "${GREEN}完成!${NC}"
            ;;
        4)
            echo -n "请输入目标进程PID: "
            read pid
            echo -e "${YELLOW}从进程 $pid 的文件中提取Cookie...${NC}"
            node index.js extract $pid -m files
            echo -e "${GREEN}完成!${NC}"
            ;;
        5)
            echo -n "请输入目标进程PID: "
            read pid
            echo -e "${YELLOW}从进程 $pid 的内存中提取Cookie...${NC}"
            node index.js extract $pid -m memory
            echo -e "${GREEN}完成!${NC}"
            ;;
        6)
            node index.js help
            ;;
        0)
            echo -e "${GREEN}感谢使用!${NC}"
            exit 0
            ;;
        *)
            echo -e "${RED}无效的选项，请重试${NC}"
            ;;
    esac
    
    echo ""
    echo -n "按Enter键继续..."
    read
    clear
done 